﻿#include "ssTexture2D.h"
//#include "ssPictureManager.h"
#include "../resmgr/ssResManager.h"


namespace StarSeeker
{

ssTexture2D::ssTexture2D()
{
	m_numLayer = 0;
}

ssTexture2D::~ssTexture2D()
{
	UnLoad();
}

void ssTexture2D::UnLoad()
{
	for (u8 i=0; i<m_numLayer; ++i)
	{
		glActiveTexture(GL_TEXTURE0 + i);
		m_pictures[i]->Nonuse();
		glDisable(GL_TEXTURE_2D);
		SS_RES_MANAGR->RecoverRes(m_pictures[i]);
	}
	m_numLayer = 0;
	m_pictures.Empty();
	glBindTexture(GL_TEXTURE_2D, 0);
}

void ssTexture2D::Use()
{
	if (m_numLayer>0)
	{
		for (u8 i=0; i<m_numLayer; ++i)
		{
			glActiveTexture(GL_TEXTURE0 + i);
			m_pictures[i]->Use();
			glEnable(GL_TEXTURE_2D);
		}
	}
}

bool ssTexture2D::Load(const char* uri)
{
	ssTextureSpirit2d* pt = SS_DYNAMIC_CAST(ssTextureSpirit2d*, SS_RES_MANAGR->QuoteResByUri(uri));
	if (pt)
	{
		m_pictures.PushBack(pt);
		++m_numLayer;
		return true;
	}
	return false;
}

void ssTexture2D::Nonuse()
{
	for (u8 i=m_numLayer-1; i>=0 && i<m_numLayer; --i)
	{
		glActiveTexture(GL_TEXTURE0 + i);
		m_pictures[i]->Nonuse();
		glDisable(GL_TEXTURE_2D);
	}
	glBindTexture(GL_TEXTURE_2D, 0);
}

StarSeeker::u32 ssTexture2D::GetHandle(u8 layer)
{
	if (layer < m_numLayer)
	{
		return m_pictures[layer]->GetHandle();
	}
	return SS_INVAILD_U32;
}

u8 ssTexture2D::GetLayers()
{
	return m_numLayer;
}

}
